package com.yunji.aggregate.service;

import com.yunji.aggregate.cache.AggregateCache;
import com.yunji.aggregate.convert.StatisticsDataConvert;
import com.yunji.aggregate.mapper.ICommonMapper;
import com.yunji.aggregate.mapper.IContainerAggregateMapper;
import com.yunji.aggregate.model.dto.container.CabinetParam;
import com.yunji.aggregate.model.dto.container.CabinetTemplateParams;
import com.yunji.aggregate.model.vo.container.*;
import com.yunji.common.enums.common.UserType;
import com.yunji.common.model.domain.SiteLocationDO;
import com.yunji.common.model.dto.LoginInfo;
import com.yunji.common.utils.bean.BeanUtils;
import com.yunji.system.context.SystemContext;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class ContainerAggregateService {
    @Resource
    private ICommonMapper commonMapper;

    @Resource
    private IContainerAggregateMapper aggregateMapper;

    public List<CabinetTemplateVO> searchTemplateVOList(CabinetTemplateParams template) {
        LoginInfo user = SystemContext.getUser();
        Long belongId = user.getBelongId();
        String userType = user.getUserType();
        if (BeanUtils.equals(userType, UserType.AGENT.getCode())) {
            template.setParentId(belongId);
        }
        if (BeanUtils.equals(userType, UserType.SITE.getCode(), UserType.STAFF.getCode())) {
            template.setBelongId(belongId);
        }
        if (template.getTypeCode() == null && template.getQrcode() != null){
            String typeCode = aggregateMapper.selectTypeCodeByQrcode(template.getQrcode());
            template.setTypeCode(typeCode);
        }
        List<CabinetTemplateVO> templateList = aggregateMapper.searchTemplateVOList(template);
        return StatisticsDataConvert.handleProductPrice(templateList, aggregateMapper);
    }

    public CabinetStatisticsVO selectContainerByCode(String code) {
        if (BeanUtils.isNull(code)) {
            return null;
        }
        CabinetParam param = new CabinetParam();
        param.setQrCode(code);
        List<CabinetStatisticsVO> list = aggregateMapper.searchCabinetList(param);
        if (BeanUtils.isNull(list)) {
            return null;
        }
        CabinetStatisticsVO cabinet = list.get(0);
        handleCabinetPrice(cabinet);
        return cabinet;
    }

    private void handleCabinetPrice(CabinetStatisticsVO cabinet) {
        Long belongId = cabinet.getBelongId();
        Long cabinetId = cabinet.getContainerId();
        List<CabinetCellStatisticsVO> cells = aggregateMapper.searchCells(belongId, cabinetId);
        cabinet.setCells(cells);
    }

    private void handleCabinetPrice(List<CabinetStatisticsVO> cabinetList) {
        List<Long> cabinetIds = cabinetList.stream().map(CabinetStatisticsVO::getContainerId).collect(Collectors.toList());
        List<CabinetCellStatisticsVO> cells = aggregateMapper.selectCells(cabinetIds);
        Map<Long, List<CabinetCellStatisticsVO>> map = new HashMap<>();
        cells.forEach(cell -> map.computeIfAbsent(cell.getContainerId(), k -> new ArrayList<>()).add(cell));
        cabinetList.forEach(cabinet -> {
            Long cabinetId = cabinet.getContainerId();
            if (!map.containsKey(cabinetId)) {
                return;
            }
            cabinet.setCells(map.get(cabinetId));
        });

    }

    public List<CabinetStatisticsVO> searchCabinet(CabinetParam cabinetParam) {
        List<CabinetStatisticsVO> list = aggregateMapper.searchCabinetList(cabinetParam);
        for (CabinetStatisticsVO vo : list) {
            if (BeanUtils.isNotNull(vo.getLocationId())){
                vo.setLocationName(AggregateCache.getLocationName(vo.getLocationId()));
                SiteLocationDO siteLocation = AggregateCache.getSiteLocation(vo.getLocationId());
                vo.setLocationTypeName(AggregateCache.getLocationTypeName(siteLocation.getLocationTypeId()));
                vo.setTypeName(AggregateCache.getContainerTypeName(vo.getTypeCode()));
            }
        }
        handleCabinetPrice(list);
        return list;
    }

    public CabinetStatisticsVO selectCabinet(Long containerId) {
        CabinetParam param = new CabinetParam();
        Long belongId = SystemContext.getBelongId();
        param.setContainerId(containerId);
        param.setBelongId(belongId);
        CabinetStatisticsVO vo = aggregateMapper.selectCabinet(param);
        if (BeanUtils.isNotNull(vo.getLocationId())){
            vo.setLocationName(AggregateCache.getLocationName(vo.getLocationId()));
            SiteLocationDO siteLocation = AggregateCache.getSiteLocation(vo.getLocationId());
            vo.setLocationTypeName(AggregateCache.getLocationTypeName(siteLocation.getLocationTypeId()));
        }
        handleCabinetPrice(vo);
        return vo;
    }

    public List<CabinetViewVO> viewContainerList(CabinetParam param) {
        LoginInfo user = SystemContext.getUser();
        Assert.isTrue(BeanUtils.isNotNull(user), "未找到用户信息");
        param.setBelongId(user.getBelongId());
        return aggregateMapper.viewContainerList(param);
    }

    public List<SpecificCabinetVO> cabinetSpecificSearch(CabinetParam param) {
        return aggregateMapper.cabinetSpecificSearch(param);
    }

    public CabinetTemplateVO selectCabinetTemplate(Long id) {
        CabinetTemplateVO vo = aggregateMapper.selectTemplateVO(id);
        LoginInfo user = SystemContext.getUser();
        Long belongId = user.getBelongId();
        List<CabinetTemplateCellVO> cells = aggregateMapper.searchCabinetTemplateCellList(belongId, Arrays.asList(vo.getTemplateId()));
        vo.setList(cells);
        return vo;
    }
}
